package com.angel.bo.auth.client.interceptor;

import com.angel.bo.auth.client.properties.AuthProperties;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author: Haibo.Mei [Haibo.Mei@ge.com]
 * @Description: Feign拦截器
 * @Date: 10:08 2018/1/30
 */
@Slf4j
public class ServiceFeignInterceptor implements RequestInterceptor{

    @Autowired
    private AuthProperties authProperties;


    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = attributes.getRequest().getHeader(authProperties.getTokenHeader());
        log.debug("服务之间feign调用时，请求的token头 {} ，token值 {}",authProperties.getTokenHeader(),token);
        //这里目的是，服务之间调用时，以为每个服务都启用了token验证机制，所以在feign请求也要加上token
        requestTemplate.header(authProperties.getTokenHeader(),token);
    }
}
